package receiver

import (
	hprose "github.com/hprose/hprose-golang/rpc"

	"github.com/upmio/horus-agent/model"
	"github.com/upmio/horus-polymerizer/log"
	"github.com/upmio/horus-polymerizer/queue"
)

func sendMetricDatas(pointslice []*model.MetricPoint) error {
	if pointslice == nil {
		return nil
	}

	queue.PutMetricPoint(pointslice)

	queue.GetDefaultKafkaMetricQue().PutMetric(pointslice)

	return nil
}

func sendEventData(event *model.EventMetric) error {
	if event == nil {
		return nil
	}

	queue.PutEvent(event)

	return nil
}

func ping() (string, error) {
	return "ping ok", nil
}

//NewHproseTCPServer is exported
func NewHproseTCPServer(ip, port string) (server *hprose.TCPServer) {
	uri := "tcp://" + ip + ":" + port + "/"
	log.Infof("server start  ip : %s  ,port :%s  ,url : %s\n", ip, port, uri)
	server = hprose.NewTCPServer(uri)

	server.AddFunction("Ping", ping)
	server.AddFunction("SendMetricDatas", sendMetricDatas)
	server.AddFunction("SendEventData", sendEventData)

	return server
}
